package com.waymap.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.waymap.common.Constant;
import com.waymap.domain.Locations;
import com.waymap.domain.User;
import com.waymap.service.UserService;
import com.waymap.utils.AuthonTokenUtil;
import com.waymap.utils.CommonEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tim on 2018/7/18.
 */
@CrossOrigin
@RestController
@RequestMapping("user")
@Api(
        value = "WMS系统",
        tags = {"WMS-用户模块"}
)
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取仓库列表
     *
     * @return
     */

    @ApiOperation(value = "获取仓库列表")
    @GetMapping("getLocList")
    @ApiImplicitParam(paramType = "query", name = "available", value = "仓库可用标识位，Y可用，N不可用", dataType = "String")
    public ResponseEntity<Map> getLocList(@RequestParam("available") String available) {
        Map map = new HashMap();
        List<Map> list = userService.getLocList(available);
        map.put("result", list);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "检测用户名密码是否正确")
    @PostMapping("checkUserInfo")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    public ResponseEntity<Map> checkUserInfo(@RequestBody User user) {


        return ResponseEntity.ok(userService.checkUserInfo(user));
    }

    @ApiOperation(value = "用户-修改密码")
    @RequestMapping(value = "/updateUserPassword", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> updateUserPassword(@ApiParam(value = "用户名") @RequestParam(required = false) String userName,
                                                  @ApiParam(value = "新密码") @RequestParam(required = false) String password,
                                                  @ApiParam(value = "老密码") @RequestParam(required = false) String oldPassword,
                                                  @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                                  @ApiParam(value = "用户Id") @RequestParam(required = false) String userId) {

        Map<String,Object> authonMap=AuthonTokenUtil.checkToken(token,userId);
        if((authonMap !=null)&&((Constant.NO).equals(authonMap.get("isAvailable")))){
            return ResponseEntity.ok(authonMap);
        }
        return userService.updateUserPassword(userName,password,oldPassword);
    }
}